package com.xbk.seckill.stock.application.dubbo;

import com.xbk.seckill.common.cache.model.SeckillBusinessCache;
import com.xbk.seckill.common.exception.ErrorCode;
import com.xbk.seckill.common.exception.SeckillException;
import com.xbk.seckill.common.model.dto.SeckillStockDTO;
import com.xbk.seckill.dubbo.interfaces.stock.SeckillStockDubboService;
import com.xbk.seckill.stock.application.service.SeckillStockBucketService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 库存Dubbo服务实现类
 *
 * @author xiexu
 * @date 2025/4/4 14:22
 */
@Slf4j
@Component
@DubboService(version = "1.0.0")
public class SeckillStockStockDubboServiceImpl implements SeckillStockDubboService {

    @Autowired
    private SeckillStockBucketService seckillStockBucketService;

    /**
     * 获取商品的可用库存
     *
     * @param goodsId
     * @param version
     * @return
     */
    @Override
    public SeckillBusinessCache<Integer> getAvailableStock(Long goodsId, Long version) {
        if (Objects.isNull(goodsId)) {
            throw new SeckillException(ErrorCode.PARAMS_INVALID);
        }
        return seckillStockBucketService.getAvailableStock(goodsId, version);
    }

    /**
     * 获取商品的库存信息
     *
     * @param goodsId
     * @param version
     * @return
     */
    @Override
    public SeckillBusinessCache<SeckillStockDTO> getSeckillStock(Long goodsId, Long version) {
        if (Objects.isNull(goodsId)) {
            throw new SeckillException(ErrorCode.PARAMS_INVALID);
        }
        return seckillStockBucketService.getSeckillStock(goodsId, version);
    }
}
